/*
 * CPISender.h
 *
 *  Created on: 2013. 12. 3.
 *      Author: mega
 */

#ifndef CPISENDER_H_
#define CPISENDER_H_

#include "../User.h"
#include "../NUtil/ThreadPool.h"

#include <string>
#include <vector>
#include <mutex>
#include <stack>

namespace ProjectZ { namespace NProcess {

class CPISender
{
public:
	struct Param
	{
		User * _user;
		std::string _json;

		std::string _result;
	};

public:
	typedef std::map<std::string, int>	CPIGIFTMAP;

public:
	CPISender();
	virtual ~CPISender();

public:
	void 	Push(Param & param);
	void	Flush();

private:
	void	work();
	//static size_t CPICallBack(void *ptr, size_t size, size_t nmemb, void *arg);
	static size_t CPICallBack_2(void *ptr, size_t size, size_t nmemb, void *arg);

	bool	RequestJSONParse(CPIGIFTMAP & giftMap, std::string & json);
	bool	ResponseJSONParse(Param & clsParam,CPIGIFTMAP & giftMap, int & reward);

	bool	SendCPIRequest(Param & clsParam);
	//bool	SendCPIRequest(std::string & json, User * pUser);
	void	SendCPIConfirm(Param & clsParam);

	void	OnEvent(User * pUser, int reward);

private:
	std::deque<Param> 	_clsParamVector;
	NUtil::ThreadPool		_clsThreadPool;
	std::mutex 			_mutex;
	std::string			_request_addr;
	std::string			_confirm_addr;
	//std::string			_result;
};

} /*NProcess*/ } /*ProjectZ*/



#endif /* CPISENDER_H_ */
